package com.chanyachting.emall.action.yellowpage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Yellowpagekeyword;
import com.chanyachting.emall.form.YellowpagekeywordForm;
import com.chanyachting.emall.util.RequestUtil;

public class YellowpagekeywordAction extends DispatchAction {
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("keywordList", Yellowpagekeyword.list());
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward listBySubcategory(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int scid=Integer.parseInt(ru.getParameter("scid", "0"));
		
		ru.setAttribute("keywordList", Yellowpagekeyword.listBySubCategory(scid));
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			YellowpagekeywordForm keywordForm=(YellowpagekeywordForm)form;
			
			if(keywordForm.validate(mapping, request).isEmpty()){
				
				Yellowpagekeyword keyword=new Yellowpagekeyword();
				
				BeanUtils.copyProperties(keyword, keywordForm);
				
				if(keyword.save()){
					
					ru.setSuccessMsg();
				}else{
					ru.setFailMsg();
				}
			}
		}
		
		ru.setAttribute("scid", ru.getParameter("subcategoryId", "0"));
		
		return mapping.findForward("create");
	}
	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			YellowpagekeywordForm keywordForm=(YellowpagekeywordForm)form;
			
			if(keywordForm.validate(mapping, request).isEmpty()){
				
				Yellowpagekeyword keyword=new Yellowpagekeyword(id);
				
				BeanUtils.copyProperties(keyword, keywordForm);
				
				if(keyword.update()){
					
					ru.setSuccessMsg();
				}else{
					ru.setFailMsg();
				}
			}
		}
		
		ru.setAttribute("keyword", new Yellowpagekeyword(id));
		
		return mapping.findForward("update");
	}
	
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Yellowpagekeyword keyword=new Yellowpagekeyword(id);
		
		if(keyword!=null) keyword.delete();
		
		ru.setAttribute("scid", keyword.getSubcategoryId());
		
		return mapping.findForward("delete");
	}

}
